package com.roy.chatmemory;

import com.roy.util.AiUtils;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;

import java.util.Scanner;

/**
 * @author 山顶洞人郑某
 * @className AiServiceChatMemory.java 基于Ai服务的聊天窗口实现
 * @date 2025 04 03
 */
public class AiServiceChatMemory {

    //多用户聊天窗口
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Assistant assistant = AiServices.builder(Assistant.class)
                .chatMemoryProvider(o -> MessageWindowChatMemory.builder()
                        .maxMessages(10)
                        .id(o)
                        //.chatMemoryStore(new InMemoryChatMemoryStore()) 默认将对话上下文存储在内存中
                        .build())
                .chatLanguageModel(OpenAiChatModel.builder().
                        apiKey(AiUtils.API_KEY)
                        .modelName(AiUtils.MODEL_NAME)
                        .build()
                )
                .build();
        while (true) {
            System.out.println("请输入你的id和想问的问题");
            String s = scanner.next();
            String s2 = scanner.next();
            String chat = assistant.chat(s, s2);
            System.out.println(chat);
        }
    }
}
